library(tidyverse)
library(plotly)

plotly vs ggplot2

mpg %>%
  mutate(cyl = as_factor(cyl)) %>% 
  ggplot(aes(displ, hwy, color = cyl)) + geom_point()

mpg %>% 
  plot_ly(x = ~displ, y = ~hwy, color =  ~factor(cyl))

Pros: - Interactive - Plotly handles multiple wide data columns (ggplot2 requies long format) - Plotly works for Python, Matlab, and Excel, among other languages - Easy layout customization - 3D charts

Cons: - Doesn’t work very well with pdf - Facet wrapping is a bit complicated compared with ggplot2 - adding legend title is difficult

ggplot(mpg, aes(displ, hwy)) + 
  geom_point(data = mutate(mpg, cyl = NULL), color = "grey75") +
  geom_point() +
  facet_wrap(vars(cyl))

ggplotly function for ggplot2 users

p <- mpg %>%
  mutate(cyl = as_factor(cyl)) %>%
  ggplot(aes(displ, hwy, color = cyl)) + geom_point()
ggplotly(p)

We won’t learn (much of)

  • ggplot2 (or should we?)

  • HTML, SVG, CSS, JavaScript

  • d3.js (R package r2d3)

Scatter plots + lines

p <- economics %>%
  sample_n(n()) %>%
  plot_ly(x = ~date, y = ~psavert)
p %>% add_paths()  # using the order of the data frame 
p %>% add_lines()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
econ <- economics %>%
  mutate(yr = year(date), mnth = month(date))

# One trace (more performant, but less interactive)
econ %>%
  group_by(yr) %>%
  plot_ly(x = ~mnth, y = ~uempmed) %>%
  add_lines(text = ~yr)
# Multiple traces (less performant, but more interactive)
econ %>% 
  mutate(yr = ordered(yr)) %>% 
  plot_ly(x = ~mnth, y = ~uempmed) %>%
  add_lines(color = ~yr)

Use Canvas rather then SVG for large dataset

plot_ly(econ, x = ~mnth, y = ~uempmed) %>%
  add_lines(color = ~ordered(yr)) %>%
  toWebGL()

Markers

  • alpha blending to see replicates
mpg %>% 
  plot_ly(x = ~cty, y = ~hwy) %>% 
  add_markers(alpha = 0.2)
  • colors for grouping
mtcars %>% 
  plot_ly(x = ~disp, y = ~mpg) %>% 
  add_markers(color = ~factor(cyl))
  • symbols
mtcars %>% 
  plot_ly(x = ~disp, y = ~mpg) %>% 
  add_markers(symbol = ~factor(cyl))

Error bar plot

mpg %>% 
  group_by(cyl) %>%
  summarize(mhwy = mean(hwy), se = sd(hwy)/sqrt(n())) %>%
  plot_ly(x = ~mhwy, y = ~factor(cyl)) %>%
  add_markers(error_x = ~list(value = se)) %>%
  layout(xaxis = list(title = "mean hwy"), yaxis = list(title = "cyl"))

Dumbbell Plot

mpg %>%
  group_by(model) %>%
  summarize(c = mean(cty), h = mean(hwy)) %>%
  mutate(model = forcats::fct_reorder(model, c)) %>%
  plot_ly() %>%
  add_segments(
    x = ~c, y = ~model,
    xend = ~h, yend = ~model, 
    color = I("gray"), showlegend = FALSE
  ) %>%
  add_markers(
    x = ~c, y = ~model, 
    color = I("blue"), 
    name = "mpg city"
  ) %>%
  add_markers(
    x = ~h, y = ~model, 
    color = I("red"),
    name  = "mpg highway"
  ) %>%
  layout(xaxis = list(title = "Miles per gallon"))

Histograms

mpg %>%
  plot_ly(x= ~hwy, color = ~factor(cyl)) %>% 
  add_histogram(histnorm = "", alpha = 0.7) %>%  # histnorm could be "", "probability", "density" and "probability density"
  layout(barmode = "overlay")  # barmode could be "overlay", "stack" and "group"
names(relig_income) %>% paste0(collapse = "\n") %>% cat
## religion
## <$10k
## $10-20k
## $20-30k
## $30-40k
## $40-50k
## $50-75k
## $75-100k
## $100-150k
## >150k
## Don't know/refused

Bar plots

mpg %>%
  mutate(cyl = as_factor(cyl)) %>% 
  count(drv, cyl) %>%
  plot_ly(x = ~drv, y = ~n, color = ~cyl) %>% 
  add_bars() %>% 
  layout(barmode = "stack")
# work with wide format directly
relig_income %>% 
  mutate(religion = as_factor(religion)) %>%
  plot_ly(y = ~religion) %>% 
  add_bars(~`<$10k`, name = "<$10k") %>%
  add_bars(~`$10-20k`, name = "$10-20k") %>%
  add_bars(~`$20-30k`, name = "$20-30k") %>%
  add_bars(~`$30-40k`, name = "$30-40k") %>%
  add_bars(~`$40-50k`, name = "$40-50k") %>%
  add_bars(~`$50-75k`, name = "$50-75k") %>%
  add_bars(~`$75-100k`, name = "$75-100k") %>%
  add_bars(~`$100-150k`, name = "$100-150k") %>%
  add_bars(~`>150k`, name = ">150k") %>%
  add_bars(~`Don't know/refused`, name = "Don't know/refused") %>%
  layout(xaxis = list(title = "count"), barmode = "stack")
# may be easier with `pivot_longer`?
relig_income %>% 
  mutate(religion = as_factor(religion)) %>%
  pivot_longer(-religion, names_to = "income", values_to = "count") %>%
  mutate(income = fct_inorder(income)) %>% 
  plot_ly(x = ~count, y = ~religion, color = ~income) %>% 
  add_bars() %>%
  layout(barmode = "stack")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

Box plots

mpg %>%
  mutate(cyl = as_factor(cyl)) %>% 
  plot_ly(x = ~drv, y = ~hwy, color = ~cyl) %>% 
  add_boxplot() %>% 
  layout(boxmode = "group")
## Warning: 'layout' objects don't have these attributes: 'boxmode'
## Valid attributes include:
## 'font', 'title', 'autosize', 'width', 'height', 'margin', 'paper_bgcolor', 'plot_bgcolor', 'separators', 'hidesources', 'showlegend', 'colorway', 'datarevision', 'uirevision', 'editrevision', 'selectionrevision', 'template', 'modebar', 'meta', 'transition', '_deprecated', 'clickmode', 'dragmode', 'hovermode', 'hoverdistance', 'spikedistance', 'hoverlabel', 'selectdirection', 'grid', 'calendar', 'xaxis', 'yaxis', 'ternary', 'scene', 'geo', 'mapbox', 'polar', 'radialaxis', 'angularaxis', 'direction', 'orientation', 'editType', 'legend', 'annotations', 'shapes', 'images', 'updatemenus', 'sliders', 'colorscale', 'coloraxis', 'metasrc', 'barmode', 'bargap', 'mapType'

Sunburst

library(gapminder)
gapminder2007 <- gapminder %>%
  filter(year == 2007)
gapminder2007 %>% 
  select(children = country, parents = continent, pop = pop) %>%
  bind_rows(
    gapminder2007 %>%
      group_by(continent) %>%
      summarize() %>% 
      transmute(children = continent, parents = "World", pop = 0)
  ) %>%
  add_row(children = "World", parents = "", pop = 0) %>% 
  plot_ly(labels = ~children, parents = ~parents, values = ~pop, type = "sunburst")
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
## Warning in bind_rows_(x, .id): binding character and factor vector, coercing
## into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector, coercing
## into character vector
## Warning in bind_rows_(x, .id): binding factor and character vector, coercing
## into character vector
## Warning in bind_rows_(x, .id): binding character and factor vector, coercing
## into character vector

Map

While plotly has map plotting feature, we will use a more popular map library leaflet.

library(leaflet)
leaflet() %>% setView(lng = -121.7405, lat = 38.5449, zoom = 13) %>% 
  addTiles()
quakes %>% 
  filter(mag > quantile(mag, 0.95)) %>% 
  leaflet() %>%
  addProviderTiles(providers$Wikimedia) %>% 
  addMarkers(~long, ~lat, label = ~mag)

Choropleths

To draw a choropleth, we first need the map data. There is a package tigris to download us map data from census.

library(leaflet)
library(tigris)
states <- states(cb = TRUE)  # lowest resolution us map
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
class(states)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
starbucks <- read_csv("starbucks.csv")
(starbucks <- starbucks %>%
  count(Province) %>%
  rename(state = Province, total = n))
## # A tibble: 54 x 2
##    state total
##    <chr> <int>
##  1 AK       42
##  2 AL       65
##  3 AR       37
##  4 AZ      391
##  5 CA     2456
##  6 CO      421
##  7 CT       97
##  8 DC       83
##  9 DE       17
## 10 FL      567
## # … with 44 more rows
states_starbucks <- states %>% 
  geo_join(starbucks, "STUSPS", "state") %>%
  subset(!is.na(total))
pal <- colorNumeric("Greens", domain=states_starbucks$total)
states_starbucks %>%
  leaflet() %>%
  setView(lng = -100, lat = 40, zoom = 4) %>% 
  addProviderTiles(providers$Wikimedia) %>% 
  addPolygons(fillColor = ~pal(total), fillOpacity = 0.7, weight = 1, smoothFactor = 0.2) %>% 
  addLegend(pal = pal, values = states_starbucks$total, position = "bottomright", title = "Starbucks")

3D charts and its family

mpg %>%
  mutate(cyl = as_factor(cyl)) %>%
  plot_ly(x = ~cty, y = ~hwy, z = ~cyl) %>%
  add_markers(color = ~cyl)
x <- seq_len(nrow(volcano)) + 100
y <- seq_len(ncol(volcano)) + 500
plot_ly(x = ~x, y = ~y, z = ~volcano) %>% 
  add_surface(colorscale = "Earth")
# heatmap
plot_ly(x = ~x, y = ~y, z = ~volcano) %>% 
  add_heatmap(colorscale = "Earth")
plot_ly(x = ~x, y = ~y, z = ~volcano) %>% 
  add_contour(colorscale = "Earth")

Animating views

df <- tibble(
  x = c(1,2,1), 
  y = c(1,2,1), 
  t = c(1,2,3)
)

df %>%
  plot_ly(x = ~x, y = ~y, frame = ~t, showlegend = F)
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
library(gapminder)
(p <- gapminder %>%
  plot_ly(x = ~lifeExp, y = ~gdpPercap, size = ~pop, color = ~continent, frame = ~year) %>%
  layout(yaxis = list(type = "log" )))
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

A more powerful (therefore difficult to master) graphic library r2d3

r2d3 is an R binding to the famous javascript library d3.js. Visit https://github.com/d3/d3/wiki/Gallery to see some of the things that d3.js is able to create.

References